const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    tabList: [],
    hasMore: true,
    keyWords: '',
  },
  onLoad(opts) {
    this.setData(opts, () => {
      this.loadTabBarList();
      wx.setNavigationBarTitle({
        title: opts.barTitle && decodeURIComponent(opts.barTitle) || '志愿服务',
      });
    });
  },
  loadTabBarList() {
    $util.request({
      url: 'entry/wxapp/partyman.home.getserviceid',
      data: {},
      success: (res) => {
        const tabList = res.data;
        this.setData({
          tabList,
          id: tabList.length > 0 && tabList[0].id,
        }, () => {
          this.loadList();
        });
      },
    });
  },
  loadList() {
    const D = this.data;
    $util.request({
      url: 'entry/wxapp/partyman.home.getservice',
      data: {
        id: D.id,
        page: D.page,
      },
      success: (res) => {
        let list = res.data;
        if (D.page > 1) {
          list = [...D.list, ...list];
        }
        this.setData({
          canshow: res.canshow,
          list,
          hasMore: list.length < parseInt(res.total, 0),
          page: list.length < parseInt(res.total, 0) ? D.page + 1 : D.page,
          loaded: true,
        });
      },
    });
  },
  loadMore() {
    this.data.hasMore && this.loadList();
  },
  onSwitchTab(e) {
    this.setData({
      id: e.detail.obj.id,
      page: 1,
    }, () => {
      this.loadList();
    });
  },
  onSearch(e) {
    this.setData({
      keyWords: e.detail.keyWords,
      page: 1,
    }, () => {
      this.loadList();
    });
  },
});